import requests
from markdownify import markdownify as md
import json
from AIAnalyzerCoze import AnalyzerCoze
from deal_result import write_result_excel_v3, load_json_result
import re
import multiprocessing
import checkurl


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36'
}
topic_info_results = []


def get_url_json(url, catalog_name, object_id):
    data = {
        'language': 'cn',
        'catalogName': catalog_name,
        'objectId': object_id
    }
    response = requests.post(url=url, headers=headers, data=data, verify=False)
    return response.json()


def get_topic_info(catalog_name):
    for tree in contents_json['value']['catalogTreeList']:
        get_article(tree)


def get_article(tree):
    if tree['isLeaf']:
        print(tree['nodeName'])
        topic_info_results.append({'nodeName': tree['nodeName'],
                                   'topic_catalog_name': catalog_name,
                                   'topic_object': tree['relateDocument'],
                                   'url': f'https://developer.huawei.com/consumer/cn/doc/{catalog_name}/{tree["relateDocument"]}'
                                   })
    if len(tree['children']) != 0:
        for child in tree['children']:
            get_article(child)
    else:
        return


def get_html(url, object_id):
    data = {
        'language': 'cn',
        'catalogName': catalog_name,
        'objectId': object_id,
        'version': ''
    }
    response = requests.post(url=url, headers=headers, data=data, verify=False)
    res = response.json()
    html = res['value']['content']['content']
    return html


def clean_markdown(md_content):
    """去除html转markdown中多余的部分"""
    cleaned_md = '# ' + md_content  # 假设输入参数名为input
    # 删除多余的等号行（至少3个等号组成的行）
    cleaned_md = re.sub(r'^={3,}$\n?', '', cleaned_md, flags=re.MULTILINE)
    # 删除多余的plaintext标记（非代码块中的独立行）
    cleaned_md = re.sub(r'^plaintext\s*$\n?', '', cleaned_md, flags=re.MULTILINE)
    # 处理多余换行（保留最多两个连续换行）
    cleaned_md = re.sub(r'\n{3,}', '\n\n', cleaned_md)
    # 修复特殊标记（如[h2]）
    cleaned_md = re.sub(r'#### \[h2\]', '###### ', cleaned_md)
    return cleaned_md


def test_article(test_range):
    flag = False
    for article in topic_info_results:
        if article['topic_object'] == test_range[0]:
            flag = True
        if flag:
            md_path = f'test/{article["topic_object"]}.md'
            with open(md_path, 'w', encoding='utf-8') as f:
                html_content = get_html(url=get_document_url, object_id=article['topic_object'])
                cache_key = ds._generate_cache_key(article['url'])
                url_result = ds._load_from_cache(cache_key)
                if not url_result:
                    try:
                        fail_urls = checkurl.main(html_content, article['url'])
                    except Exception as e:
                        print(e)
                        checkurl.kill_chromium_process('chromium')
                        fail_urls = []
                else:
                    fail_urls = url_result['链接问题']
                # fail_urls = []
                md_content = md(html_content)
                md_content = clean_markdown(md_content)
                f.write(md_content)
            test[article['topic_object']] = {
                "title": article['nodeName'],
                'url': article['url'],
                'product': article['topic_catalog_name'],
            }
            ds.analyze_html_file(md_path, article['url'], catalog_name, article['nodeName'], fail_urls)
        if article['topic_object'] == test_range[1]:
            break


if __name__ == '__main__':
    test = {}
    catalog_name = "best-practices"
    object_id = "bpta-landscape-and-portrait-development"
    language = 'cn'
    get_catalog_tree_url = 'https://svc-drcn.developer.huawei.com/community/servlet/consumer/cn/documentPortal/getCatalogTree'
    contents_json = get_url_json(get_catalog_tree_url, catalog_name, object_id)
    get_topic_info(catalog_name)
    get_document_url = "https://svc-drcn.developer.huawei.com/community/servlet/consumer/cn/documentPortal/getDocumentById"
    test_guide = ('bpta-landscape-and-portrait-development', 'bpta-landscape-and-portrait-development')
    test_guide1 = ('web-component-overview', 'web-white-screen')
    test_guide2 = ('avsession-overview', 'avsession-recommendation')
    ds = AnalyzerCoze()
    test_article(test_guide)
    # test_article(test_guide1)
    # test_article(test_guide2)
    with open('test.json', 'w', encoding='utf-8') as f:
        json.dump(test, f, ensure_ascii=False, indent=2)
    analyzer_results = load_json_result('.code_cache')
    write_result_excel_v3(analyzer_results)
